package com.chillis.standings.domain.table;

import android.content.Context;
import com.chillis.standings.R;
import com.chillis.standings.domain.exceptions.FormatNotFoundException;

import java.util.ArrayList;
import java.util.List;

public class TableSettings {
   private static final int MAX_AVAILABLE_PARTICIPANTS_COUNT_FOR_ROUND_FORMAT = 128;

   public enum Format {
      Round,
      Olympic;

      public static Format fromString(Context context, String string) {
         Format result;

         if (string.equals(context.getString(R.string.format_round))) {
            result = Format.Round;
         } else if (string.equals(context.getString(R.string.format_olympic))) {
            result = Format.Olympic;
         } else {
            throw new FormatNotFoundException();
         }

         return result;
      }

      public String toString(Context context) {
         String result;

         if (this == Round) {
            result = context.getString(R.string.format_round);
         } else if (this == Olympic) {
            result = context.getString(R.string.format_olympic);
         } else {
            throw new FormatNotFoundException();
         }

         return result;
      }

      public static Format byIndex(Context context, int index) {
         String stringRepresentation = availableFormats(context)[index];

         return fromString(context, stringRepresentation);
      }
   }

   public static String[] availableFormats(Context context) {
      List<String> formats = new ArrayList<String>();

      for (Format format : Format.values()) {
         formats.add(format.toString(context));
      }

      return formats.toArray(new String[formats.size()]);
   }

   public static String[] availableParticipantsCountsForRoundFormat() {
      int availableParticipantsCount = 2;
      List<String> counts = new ArrayList<String>();

      while (availableParticipantsCount <= MAX_AVAILABLE_PARTICIPANTS_COUNT_FOR_ROUND_FORMAT) {
         counts.add(String.valueOf(availableParticipantsCount));

         availableParticipantsCount *= 2;
      }

      return counts.toArray(new String[counts.size()]);
   }

}
